package com.qyhl.webtv.module_user.login.wx.binding;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.user.UserInfoBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.module_user.R;
import com.qyhl.webtv.module_user.R2;
import com.qyhl.webtv.module_user.util.LoginUtils;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

@Route(path = ARouterPathConstant.BINDING_ACTIVITY)
public class BindingActivity extends BaseActivity implements BindingContract.BindingView {
    private String openid;
    private String name;
    private String gender;
    private String iconurl;

    private BindingContract.BindingPresenterModel presenterModel;
    private CountDownTimer mTimer;

    @BindView(R2.id.editname)
    EditText editname;
    @BindView(R2.id.phononum)
    EditText phononum;
    @BindView(R2.id.editcode)
    EditText editcode;
    @BindView(R2.id.editinvitation)
    EditText editinvitation;
    @BindView(R2.id.btncode)
    Button btncode;
    @BindView(R2.id.binding)
    Button binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.login_activity_binding;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        inits();
    }

    @Override
    protected void setListener() {

    }


    private void inits() {
        presenterModel = new BindingPresenterModel(this);
        openid = getIntent().getExtras().getString("uid");
        name = getIntent().getExtras().getString("name");
        gender = getIntent().getExtras().getString("gender");
        if (TextUtils.isEmpty(gender)) {
            gender = "C";
        } else if (gender.equals("男")) {
            gender = "M";
        } else if (gender.equals("女")) {
            gender = "F";
        } else {
            gender = "C";
        }
        iconurl = getIntent().getExtras().getString("iconurl");

        editname.setText(name);
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(editname.getText().toString()) ||
                        TextUtils.isEmpty(phononum.getText().toString()) ||
                        TextUtils.isEmpty(editcode.getText().toString())) {
                    binding.setEnabled(false);
                } else {
                    binding.setEnabled(true);
                }
            }
        };
        editname.addTextChangedListener(textWatcher);
        phononum.addTextChangedListener(textWatcher);
        editcode.addTextChangedListener(textWatcher);

        //配置计时器
        mTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                btncode.setText(millisUntilFinished / 1000 + "s后重新获取");
            }

            @Override
            public void onFinish() {
                resetCodeBtn();
            }
        };
    }

    private void resetCodeBtn() {
        if (mTimer != null) {
            mTimer.cancel();
        }
        btncode.setText("获取验证码");
        btncode.setEnabled(true);
    }

    @OnClick({R2.id.login_back, R2.id.binding, R2.id.btncode})
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.login_back) {
            finish();

        } else if (i == R.id.binding) {
            if (StringUtils.containsEmoji(editname.getText().toString())) {
                Toasty.normal(this, "昵称含非法字符", Toast.LENGTH_SHORT).show();
            } else {
                if (StringUtils.isMobileNO(phononum.getText().toString().trim())) {
                    showSimpleLoading();
                    presenterModel.gobinding(openid,
                            editname.getText().toString().trim(),
                            phononum.getText().toString().trim(),
                            editcode.getText().toString().trim(),
                            editinvitation.getText().toString(),
                            gender,
                            iconurl);
                } else {
                    Toasty.normal(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                }
            }

        } else if (i == R.id.btncode) {
            getCode();

        } else {
        }
    }

    private void getCode() {
        String code = phononum.getText().toString();
        if (StringUtils.isMobileNO(code)) {
            mTimer.start();
            btncode.setEnabled(false);
            presenterModel.getCode(code);
        } else {
            Toasty.normal(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void getCodeError(String s) {
        Toasty.normal(this, s, Toast.LENGTH_SHORT).show();
        resetCodeBtn();
    }

    @Override
    public void getCodeSuccess() {
        Toasty.normal(this, "验证码已发送，请耐心等待...", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void bindingError(String s) {
        dismissLoading();
        Toasty.normal(this, s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void bindingSuccess(UserInfoBean bean) {
        dismissLoading();
        Toasty.normal(this, "登录成功！", Toast.LENGTH_SHORT).show();
        LoginUtils.setloginSuccess(bean, ActionConstant.ACCOUNTLOGIN);
        setResult(666);
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //baidu
        //umeng
        MobclickAgent.onPageStart("微信绑定");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //baidu
        //umeng
        MobclickAgent.onPageEnd("微信绑定");

        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        super.onDestroy();
    }


}
